import requests, re, execjs
from bs4 import BeautifulSoup
from Crypto.Cipher import AES
import binascii

class Load():
    BLOCK_SIZE = 16
    def __init__(self, cookie):
        with open('app.js') as f:
            self.decode = execjs.compile(f.read())
        cookies = {}
        for line in cookie.split(';'):  # 按照字符：进行划分读取
            # 其设置为1就会把字符串拆分成2份
            name, value = line.strip().split('=', 1)
            cookies[name] = value  # 为字典cookies添加内容
        self.cookie = cookies

    def load(self, url = ''):

        content = requests.get(url, cookies=self.cookie).content
        with open('1.html', 'wb') as f:
            f.write(content)
        html = BeautifulSoup(content.decode(), 'lxml')
        title = html.select('title')[0].string
        content = html.select('.data-trda')[0].attrs['value']
        key = html.select('.data-trdkk')[0].attrs['value']
        key = key.replace('s=', '')
        key = key.split('.')
        # key[0] = binascii.a2b_hex(key[0])
        # key[1] = binascii.a2b_hex(key[1])
        # aes = AES.new(key[0], AES.MODE_CBC, key[1],)
        # content = aes.decrypt(binascii.a2b_base64(content).rstrip())
        # return [title, content]
        content = self.de2(content, key)
        # content = content.decode()
        content = re.sub(r"\s{2,}|<p>|<P>", '', content)
        content = re.sub(r"</p>|</P>", "\n", content)

        return [title, content]

    def de2(self, content, key):
        return self.decode.call('decode',content, key[0], key[1])



cookie = 'PHPSESSID=a5hevgbfaqmss4impvhhhcmoq2; uuid=984beafb-3adb-c55a-1149-621bf660a664; sexstyle=1; first_index=1; Hm_lvt_400a68afd5dc8d225ea5ed66420956ed=1529905210; Hm_lvt_50b4e823049b69a49522a7c3df9b4d7c=1529910151,1529912563,1529912773,1529936808; Hm_lpvt_400a68afd5dc8d225ea5ed66420956ed=1529937389; _article_id=13488; _stat=eyJzaG9ydF9ib29rX2NoYXB0ZXJfdW5pcXVlIjoxLCJzaG9ydF9ib29rX2RfY2xpY2tfdW5pcXVlIjoxLCJpbmRleF91bmlxdWUiOjE1Mjk5ODUxMzYsImluZGV4X21fdW5pcXVlIjoxNTI5OTg1MTM2fQ%3D%3D; qqlogin_state=921c29e36edad4251d0cd6a50cc20e67; qqlogin_backUrl=http%3A%2F%2Fm.anyew.cn%2Flogin-qq_return; pauserid=8987158; pausername=%F0%9F%90%BD2419; papasswordhash=0324da809b03728334ec7da9efa93fdd; Hm_lpvt_22457af4ec023e0e8bbf351791a5c811=1529985389; Hm_lvt_22457af4ec023e0e8bbf351791a5c811=1529985389; Hm_lpvt_50b4e823049b69a49522a7c3df9b4d7c=1529985391'

# content = Load(cookie)
# content = content.load('http://m.anyew.cn/chapter/13488/48.html')

# print(content[0])
# print()
# print(content[1])



